//
//  CloudTableViewCell.m
//  gdemm
//
//  Created by 乔明超 on 16/9/26.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "CloudTableViewCell.h"
#import "Masonry.h"
#import "DocManager.h"
#import "StringUtil.h"
#import "Utils.h"
#import "FileUtil.h"
#import "GetURLFileLength.h"
#import "InboxManager.h"
@interface CloudTableViewCell()
@property (strong, nonatomic) UILabel *titleLable;
@property (strong, nonatomic) UILabel *timeLable;
@property (strong, nonatomic) UIView *readIcon;
@property (strong, nonatomic) UIImageView *fileTypeImageView;
@property (strong,nonatomic) DocManager *docMgr;
@property(nonatomic,strong)NSString * info;
@end
@implementation CloudTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupTableViewCell];
    }
    
    return self;
}

- (DocManager *)docMgr{
    if (_docMgr == nil) {
        _docMgr = [DocManager getInstance];
    }
    return _docMgr;
}
- (void)setFileInfo:(FileInfo *)fileInfo{
    _fileInfo  = fileInfo;
    //获取文件的名字
    _titleLable.text = [StringUtil getFileName:fileInfo.fileName];
    _timeLable.text = [self getTime:fileInfo.fileTime];
    
    
    if ([FileUtil isFileExistWithFilePath:fileInfo.localPath]) {
        self.info = [NSString stringWithFormat:@"%@ 发送给 %@",[FileUtil getFileSize:fileInfo.localPath],[[UserDefaultsUtil shareInstance]getString:LC_USERNAME]];
        _detailLable.text = self.info;
    }else{
        GetURLFileLength *getFileSize = [[GetURLFileLength alloc]init];
        [getFileSize getUrlFileLength:fileInfo.downloadUrl withResultBlock:^(NSString *length, NSError *error) {
            self.info = [NSString stringWithFormat:@"%@ 发送给 %@",length,[[UserDefaultsUtil shareInstance]getString:LC_USERNAME]];
            dispatch_async(dispatch_get_main_queue(), ^{
                _detailLable.text = self.info;
            });
            
        }];
    }
    
    NSString *fileTypeIconPath = [self.docMgr getPathMappedIconId:_fileInfo.fileName];
    _fileTypeImageView.image = [Utils imageWithKey:fileTypeIconPath];
    
    if ([[InboxManager getInstance]isLocalFileExist:fileInfo.fileName]) {
        _readIcon.hidden = YES;
    }else{
        _readIcon.hidden = NO;
    }
    if (fileInfo.selState) {
        if (!fileInfo.selected) {
            _fileTypeImageView.image = [Utils getImageByDevce:@"other_icon_notsel"];
        }else{
            _fileTypeImageView.image = [Utils imageWithKey:@"other_icon_sel"];
        }
        
    }else{
        fileInfo.selected = NO;
    }
    
}
- (void)setupTableViewCell{
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    //添加文件类型imageView
    _fileTypeImageView = [[UIImageView alloc]init];
    [self addSubview:_fileTypeImageView];
    
    //未读图标View
    _readIcon = [[UIView alloc]init];
    _readIcon.backgroundColor = [Utils UIColorFromHex:0XFACF4D];
    _readIcon.layer.cornerRadius = 5;
    [self addSubview:_readIcon];
    
    //标题label
    _titleLable = [[UILabel alloc]init];
    _titleLable.textColor = [Utils UIColorFromHex:0Xecfaff];
    _titleLable.numberOfLines = 1;
    _titleLable.lineBreakMode = NSLineBreakByWordWrapping;
    [self addSubview:_titleLable];
    
    //详细细节label
    _detailLable = [[UILabel alloc]init];
    _detailLable.textColor = [Utils UIColorFromHex:0X8db0de];
    [self addSubview:_detailLable];
    
    //时间label
    _timeLable = [[UILabel alloc]init];
    _timeLable.textAlignment = NSTextAlignmentRight;
    _timeLable.textColor = [Utils UIColorFromHex:0Xffffff];
    [self addSubview:_timeLable];
    
    //线条view
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [Utils UIColorFromHex:0X4c84cc];
    [self addSubview:self.lineView];
    if ([DeviceTypeJudge isPadDeviceType]) {
        [self setupIPadTableViewCell];
    }else{
    
        [self setupIPhoneTableViewCell];
    }

}

- (void)setupIPadTableViewCell{
    
    
    //文件类型imageView约束
    [_fileTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(48);
    }];
    
    //未读图标imageView约束
    [_readIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.width.height.mas_equalTo(10);
        make.left.mas_equalTo(_fileTypeImageView.mas_right).offset(14);
    }];
    
    //标题titleLabel约束
    _titleLable.font = [UIFont systemFontOfSize:18];
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.mas_centerY);
        make.left.mas_equalTo(_readIcon.mas_right).offset(10);
        make.width.mas_equalTo(600);
    }];
    //时间timeLabel约束
    _timeLable.font = [UIFont systemFontOfSize:16];
    [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.mas_centerY);
        make.right.mas_equalTo(-16);
    }];
    
    //详细细节detailLabel约束
    _detailLable.font = [UIFont systemFontOfSize:14];
    [_detailLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLable.mas_bottom);
        make.left.mas_equalTo(_titleLable.mas_left);
    }];
    
    //线条line约束
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLable.mas_left);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];

}

- (void)setupIPhoneTableViewCell{

    //文件类型imageView约束
    [_fileTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(35);
    }];
    
    //未读图标imageView约束
    [_readIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(17);
        make.width.height.mas_equalTo(10);
        make.left.mas_equalTo(_fileTypeImageView.mas_right).offset(10);
    }];
    
    //时间timeLabel约束
    _timeLable.font = [UIFont systemFontOfSize:11];
    [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.mas_centerY);
        make.right.mas_equalTo(-12);
        make.width.mas_equalTo(40);
    }];
    
    //标题titleLabel约束
    _titleLable.font = [UIFont systemFontOfSize:14];
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.mas_centerY);
        make.left.mas_equalTo(_readIcon.mas_right).offset(6);
        make.right.mas_equalTo(_timeLable.mas_left).offset(-10);
        
    }];
    
    
    //详细细节detailLabel约束
    _detailLable.font = [UIFont systemFontOfSize:11];
    [_detailLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLable.mas_bottom);
        make.left.mas_equalTo(_titleLable.mas_left);
    }];
    
    //线条line约束
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLable.mas_left);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(NSString * )getTime:(NSString *) date{
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSDate *showDate =[dateFormat dateFromString:date];
    NSCalendar * calendar = [NSCalendar currentCalendar];
   NSDateComponents * components =  [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:showDate toDate:[NSDate date] options:NSCalendarWrapComponents];
    if (components.year) {
        [dateFormat setDateFormat:@"MM-dd HH:mm"];
    }else{
        if (components.month) {
            [dateFormat setDateFormat:@"MM-dd HH:mm"];
        }else{
            [dateFormat setDateFormat:@"HH:mm"];
        }
    }
    
    return [dateFormat stringFromDate:showDate];
    
    
}

@end
